import logging
import os

WORK_DIR = os.path.dirname(__file__)


def init_log():
    logger = logging.getLogger("spel4python")
    logger.setLevel(logging.INFO)

    fh = logging.FileHandler("%s/spel4python.log" % WORK_DIR, mode="w")
    fh.setLevel(logging.INFO)

    formatter = logging.Formatter(
        "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
    )
    fh.setFormatter(formatter)

    logger.addHandler(fh)
    return logger


def init_ir_log(opt_desc: str):
    logger = logging.getLogger("ir_" + opt_desc)
    logger.setLevel(logging.INFO)

    fh = logging.FileHandler("%s/ir_%s.log" % (WORK_DIR, opt_desc), mode="w")
    fh.setLevel(logging.INFO)

    formatter = logging.Formatter("%(message)s")
    fh.setFormatter(formatter)

    logger.addHandler(fh)
    return logger


LOGGER = init_log()

IR_LOGGER = init_ir_log("unopt")
OPT_IR_LOGGER = init_ir_log("opt")


def write_both_ir_log(msg):
    IR_LOGGER.info(msg)
    OPT_IR_LOGGER.info(msg)


def write_unopt_ir(msg):
    IR_LOGGER.info(msg)


def write_opt_ir(msg):
    OPT_IR_LOGGER.info(msg)
